package com.sun.stockexchangesimulate.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.stockexchangesimulate.domain.Investor;
import com.sun.stockexchangesimulate.service.InvestorService;
import com.sun.stockexchangesimulate.mapper.InvestorMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
* @author sun
* @description 针对表【investor(股票投资者表)】的数据库操作Service实现
* @createDate 2024-10-23 14:54:34
*/
@Service
public class InvestorServiceImpl extends ServiceImpl<InvestorMapper, Investor>
    implements InvestorService {

    @Resource
    private InvestorMapper investorMapper;

    /**
     * 更新账户余额
     *
     * @param investorId  买家账户
     * @param investorId1 卖家账户
     * @param sellPrice   卖出价格
     */
    @Override
    public void updateAccount(Integer investorId, Integer investorId1, BigDecimal sellPrice) {

        Investor buy = investorMapper.selectById(investorId);
        Investor sell = investorMapper.selectById(investorId1);
        buy.setCash(buy.getCash().subtract(sellPrice));
        sell.setCash(sell.getCash().add(sellPrice));
        sell.setAvailableCash(sell.getAvailableCash().add(sellPrice));
        investorMapper.updateById(buy);
        investorMapper.updateById(sell);
    }

    /**
     * 更新投资者的可用资金
     *
     * @param investorId -投资者id
     * @param amount     -金额
     */
    @Override
    public void updateAvailableAmount(Integer investorId, BigDecimal amount) {

        Investor investor = investorMapper.selectById(investorId);
        investor.setAvailableCash(investor.getAvailableCash().add(amount));
        investorMapper.updateById(investor);
    }
}




